<?php
/**
 * Created by PhpStorm.
 * User: Daxu
 * Date: 2018/3/25
 * Time: 19:54
 */

namespace App\Http\Controllers\API;


use App\Http\Controllers\Controller;
use App\Http\Requests\FinancialStaffInfoRequest;
use App\Http\Resources\FinancialStaffInfoResource;
use App\Model\FinancialStaffInfo;
use App\Model\Role;
use App\Model\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class FinancialStaffInfoController extends Controller
{
    public function index(Request $request)
    {
        $keyword = $request->get('name', '');
        $models = FinancialStaffInfo::query()->when($keyword, function ($query) use ($keyword) {
            $query->where('name', 'like', '%' . $keyword . '%');
        })->paginate();
        return FinancialStaffInfoResource::collection($models);
    }


    /**
     * 添加实例
     * @param Request $request
     * @return mixed
     */
    public function store(FinancialStaffInfoRequest $request)
    {
        $input = $request->all();
        try {
            DB::beginTransaction();
            $user = new User();
            $userInput = [
                'username' => $input['username'],
                'password' => bcrypt($input['password']),
                'api_token' => str_random(60),
                'type' => 'financial',
            ];
            $user->fill($userInput);
            if (!$user->save()) {
                throw new \Exception('用户保存失败');
            }
            $role = Role::select('id')->where('name','financial')->get()->toArray();
            $user->attachRoles($role);
            $model = new FinancialStaffInfo();
            $data = [
                'users_id' => $user->id,
                'code' => $input['code'],
                'name' => $input['name'],
                'judicial_name' => $input['judicial_name'],
                'tel' => $input['tel'],
            ];
            $model->fill($data);
            if (!$model->save()) {
                throw new \Exception('个人信息保存失败');
            }
            DB::commit();
            return new FinancialStaffInfoResource($model);
        } catch (\Exception $e) {
            DB::rollBack();
            return response()->json($e->getMessage(), 406);
        }
    }


    /**
     * 修改实例
     * @param $id
     * @param Request $request
     * @return mixed
     */
    public function update($id, FinancialStaffInfoRequest $request)
    {
        $model = $this->findModel($id);
        $model->fill($request->all());
        $model->save();
        return new FinancialStaffInfoResource($model);
    }


    /**
     * 删除
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        $model = $this->findModel($id);
        $user = $model->user;
        try {
            $user->delete();
            $model->delete();
            return response()->json(['state' => 'success']);
        } catch (\Exception $e) {
            return response()->json(['state' => 'failed', 'message' => $e->getMessage()], 406);
        }
    }


    /**
     * 返回单个实例
     * @param User $user
     * @return mixed
     */
    public function show($id)
    {
        $model = $this->findModel($id);
        return new FinancialStaffInfoResource($model);
    }


    /**
     * @param $id
     * @return Staff
     */
    public function findModel($id)
    {
        $model = FinancialStaffInfo::query()->where('id', $id)->first();
        return $model;
    }
}
